<template>
  <div>
    <h1>当前求和为:{{ $store.state.sum }}</h1>
    <h1>放大十倍之后的和为:{{ $store.getters.bigSum }}</h1>
    <h2>我在{{ $store.state.school }},正在学习{{ $store.state.subject }}</h2>
    <select v-model.number="num">
      <option value="1">1</option>
      <option value="2">2</option>
      <option value="3">3</option>
    </select>
    <button @click="increment">+</button>
    <button @click="decrement">-</button>
    <button @click="incrementOdd">当前求和为奇数再加</button>
    <button @click="incrementWait">等一等再加</button>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import { mapGetters } from 'vuex'
export default {
  name: 'Count',
  data() {
    return {
      num: 1, //被操作的数
    }
  },
  methods: {
    increment() {
      this.$store.commit('ADD', this.num)
    },
    decrement() {
      this.$store.commit('SUB', this.num)
    },
    incrementOdd() {
      this.$store.dispatch('addOdd', this.num)
    },
    incrementWait() {
      this.$store.dispatch('addWait', this.num)
    },
  },
  computed: {
    //自己去写计算属性
    // sum() {
    //   return this.$store.state.school
    // },
    // school() {
    //   return this.$store.state.school
    // },
    // subject() {
    //   return this.$store.state.subject
    // },
    //借助mapState生成计算属性，从state中读取数据（对象写法）
    // ...mapState({ sum: 'sum', school: 'school', subject: 'subject' }),
    //借助mapState生成计算属性，从state中读取数据（数组写法）
    ...mapState(['sum', 'school', 'subject']),

    //**************** */
    // bidSum() {
    //   return this.$store.getters.bigSum
    // },
    //借助mapGetters生成计算属性（对象写法）
    // ...mapGetters({ bigSum: 'bigSum' }),
    ...mapGetters(['bigSum']),
  },
}
</script>

<style>
button {
  margin-left: 5px;
}
</style>
